package com.gem.service.impl;

import com.gem.domain.CCooperationResult;
import com.gem.domain.CCrCompany;
import com.gem.mapper.CCooperationResultMapper;
import com.gem.mapper.CCrCompanyMapper;
import com.gem.service.CCooperationResultService;
import com.gem.util.ConditionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.NoTransactionException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * @author Apolar_GuanYH-管阳红
 * Description: 合作成果接口实现
 * create 2019/10/24 23:20
 * Modified By:
 */
@Service
public class CCooperationResultServiceImpl implements CCooperationResultService {


    //自动注入CCrCompanyMapper
    private CCrCompanyMapper cCrCompanyMapper;

    @Autowired
    public void setcCrCompanyMapper(CCrCompanyMapper cCrCompanyMapper) {
        this.cCrCompanyMapper = cCrCompanyMapper;
    }

    //自动注入CCooperationResultMapper
    private CCooperationResultMapper ccrMapper;

    @Autowired
    public void setcCooperationResult(CCooperationResultMapper ccrMapper) {
        this.ccrMapper = ccrMapper;
    }

    //查询所有
    @Override
    public List<CCooperationResult> selectAllCR() {
        return ccrMapper.selectAllCR();
    }

    //添加
    @Override
    public boolean addCRService(CCooperationResult cCooperationResult) {
        if (cCooperationResult.getCrTopicUnitprice() == null) {
            cCooperationResult.setCrTopicUnitprice(0.00);
        }
        if (cCooperationResult.getCrTopicTotalprice() == null) {
            cCooperationResult.setCrTopicTotalprice(0.00);
        }
        if (cCooperationResult.getCrPersonNum() == null) {
            cCooperationResult.setCrPersonNum(0);
        }
        if(cCooperationResult.getCrCourseGrade()==null){
            cCooperationResult.setCrCourseGrade("0");
        }
        if (cCooperationResult.getCrCourseEvaluation()==null){
            cCooperationResult.setCrCourseEvaluation("暂无评价哦！");
        }
        return ccrMapper.addCR(cCooperationResult);
    }

    //修改
    @Override
    public boolean modifyCRService(CCooperationResult cCooperationResult) {
        if (cCooperationResult.getCrTopicUnitprice() == null) {
            cCooperationResult.setCrTopicUnitprice(0.00);
        }
        if (cCooperationResult.getCrTopicTotalprice() == null) {
            cCooperationResult.setCrTopicTotalprice(0.00);
        }
        if (cCooperationResult.getCrPersonNum() == null) {
            cCooperationResult.setCrPersonNum(0);
        }
        return ccrMapper.modifyCR(cCooperationResult);
    }

    //按编号查询
    @Override
    public CCooperationResult selectCRByIdService(Integer crId) {
        return ccrMapper.selectCRById(crId);
    }

    //单个删除
    @Override
    public boolean deleteCRByIdService(Integer crId) {
        return ccrMapper.deleteCRById(crId);
    }

    //批量删除
    @Override
    @Transactional
    public boolean deleteCRByIdsService(String[] crIdArr) {
        try {
            boolean flag = true;
            if (crIdArr.length != 0) {
                for (String crId : crIdArr) {
                    if (!crId.trim().equals("")) {
                        if (!deleteCRByIdService(Integer.parseInt(crId))) {
                            flag = false;
                            break;
                        }
                    }
                }
                if (!flag) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                }
            }
            return flag;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    //分页显示
    @Override
    public PageInfo<CCooperationResult> selectCRByPage(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        return new PageInfo<>(ccrMapper.selectAllCR());
    }

    //查询分页显示
    @Override
    public PageInfo<CCooperationResult> selectCRByConditionByPage(int currentPage, int pageSize, ConditionUtil cu) {
        PageHelper.startPage(currentPage, pageSize);
        return new PageInfo<>(ccrMapper.selectCRByCondition(cu));
    }

    //查询所有其他合作成果（分页）
    @Override
    public PageInfo<CCooperationResult> findOther(int coId, int currentPage, int pageSize) {
        PageInfo<CCooperationResult> pageInfo = null;
        try {
            PageHelper.startPage(currentPage, pageSize);
            pageInfo = new PageInfo<>(ccrMapper.findOther(coId));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    @Transactional
    public Integer saveOther(Integer coId, CCooperationResult result) {

        try {
            ccrMapper.saveOther(result);
            ccrMapper.saveCother(result.getCrId(), coId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return 1;
    }

    @Override
    @Transactional
    public Integer deleteOtherById(Integer crId) {
        try {
            ccrMapper.deleteOtherById(crId);
            ccrMapper.deleteCother(crId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return 1;
    }

    @Override
    public CCooperationResult findOtherById(Integer crId) {
        return ccrMapper.findOtherById(crId);
    }

    @Override
    public Integer updateOtherById(CCooperationResult result) {
        return ccrMapper.updateOtherById(result);
    }

    @Override
    @Transactional
    public Integer deleteAllOther(int[] crIds) {
        try {
            ccrMapper.deleteAllOther(crIds);
            ccrMapper.deleteAllCother(crIds);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return 1;
    }

    //上传文件
    @Override
    public boolean uploadExcelService(List<CCooperationResult> list) {
        try {
            boolean flag = true;
            for (CCooperationResult cCooperationResult : list) {
                if (!ccrMapper.addCR(cCooperationResult)) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                return true;
            } else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        } catch (NoTransactionException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return false;
    }

    @Override
    public boolean uploadExcelSheetsService(List<CCooperationResult> list, List<CCrCompany> companylist) {
        try {
            boolean flag = true;
            for (CCooperationResult cCooperationResult : list) {
                if (!ccrMapper.addCR(cCooperationResult)) {
                    flag = false;
                    break;
                }
            }
            if(flag){
                for (CCrCompany cCrCompany : companylist) {
                    if (!cCrCompanyMapper.addCC(cCrCompany)) {
                        flag = false;
                        break;
                    }
                }
            }

            if (flag) {
                return true;
            } else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        } catch (NoTransactionException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return false;
    }


}
